/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author liuzhehuan
 */
import java.sql.*;
//import java.util.logging.Level;
//import java.util.logging.Logger;

public class DBData {

//    Connection c;

//    public DBData() throws InstantiationException, IllegalAccessException, SQLException {
//        try {
//            Class.forName("com.mysql.jdbc.Driver").newInstance();
//            String URL = "jdbc:mysql://127.0.0.1:3306/e-shop?user=root&password=bbc44944";
//            c = DriverManager.getConnection(URL);
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(DBData.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    public void play() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String URL = "jdbc:mysql://127.0.0.1:3306/e-shop?user=root&password=bbc44944";
            Connection c = DriverManager.getConnection(URL);
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM customers");
            while (rs.next()) {
                String name = rs.getString("Name");
                String pss = rs.getString("passWord");
                System.out.println("Name: " + name + " and nbr " + pss);
            }
            c.close();
        } catch (Exception e) {
            System.err.println("ERROR: " + e);
        }
    }
    public void insert(String name, String pass){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String URL = "jdbc:mysql://127.0.0.1:3306/e-shop?user=root&password=bbc44944";
            Connection c = DriverManager.getConnection(URL);
            Statement st = c.createStatement();
            st.executeUpdate("INSERT INTO customers(Name, passWord) value('"+ name +"','"+ pass + "')");
            c.close();
        }catch(Exception e){
            System.out.println("ERROR: " + e);
        }
    }
    public void get(String name, String passWord){
         try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String URL = "jdbc:mysql://127.0.0.1:3306/e-shop?user=root&password=bbc44944";
            Connection c = DriverManager.getConnection(URL);
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("SELECT passWord FROM customers WHERE Name = '"+ name +"'");
            c.close();
        }catch(Exception e){
            System.out.println("ERROR: " + e);
        }
    }
}
